package shplate

class TrackBackStatisticsController {

    def dynamicQueryService
    def exportFileService

    def index(Integer max) {
        def cellList = org.kdx.DataUtils.getCellList()
        def pageSizeList = org.kdx.DataUtils.getPageSizeList()
        [totalCount: 5, pageSize: 20, pageCurrent: 1, orderField: 'id', orderDirection: 'asc', pageSizeList: pageSizeList, cellList: cellList]
    }

    def trackBackStatistics() {
        params.startT = DateUtil.getStartDate(params.startT).startDate
        params.endT = DateUtil.getEndDate(params.endT).endDate
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 100
        params.startno = (pageSize * (pageCurrent - 1)) + 1
        params.endno = pageSize * pageCurrent
        def pageSizeList = [10, 20, 50, 100]
        params.state = params.state ? Arrays.asList(params.state) : (params['state[]'] ? Arrays.asList(params['state[]']) : '')
        params.gzzx = params.gzzx ? Arrays.asList(params.gzzx) : (params['gzzx[]'] ? Arrays.asList(params['gzzx[]']) : '')
        params.transType = params.transType ? Arrays.asList(params.transType) : (params['transType[]'] ? Arrays.asList(params['transType[]']) : '')
        def trackBackStatistics = []
        def tableHeadList = []
        def totalCount = 0
//        params.SYS_QUERY_NAME = 'trackBackStatisticsDocId'
//        def trackBackStatisticsDocId = dynamicQueryService.queryForList(params)
        // 订单状态
        params.SYS_QUERY_NAME = 'trackBackStatisticsState'
        def trackBackStatisticsState = dynamicQueryService.queryForList(params)
        // 客户名称
        params.SYS_QUERY_NAME = 'trackBackStatisticsKhmc'
        def trackBackStatisticsKhmc = dynamicQueryService.queryForList(params)
        // 清洗属性
        params.SYS_QUERY_NAME = 'trackBackStatisticsQxsx'
        def trackBackStatisticsQxsx = dynamicQueryService.queryForList(params)
        // 工作中心 阶段
        params.SYS_QUERY_NAME = 'trackBackStatisticsGZZX'
        def trackBackStatisticsGZZX = dynamicQueryService.queryForList(params)
        // 批次事务类型
        params.SYS_QUERY_NAME = 'trackBackStatisticsSWLX'
        def trackBackStatisticsSWLX = dynamicQueryService.queryForList(params)

        if ('Excel'.equals(params.EXPORT_CONTENT_NAME)) {
            params.SYS_QUERY_NAME = 'trackBackStatistics'
            trackBackStatistics = dynamicQueryService.queryForList(params)
            if (trackBackStatistics.size() > 0) {
                tableHeadList = trackBackStatistics[0].keySet()
            }
            params.SYS_QUERY_NAME = 'trackBackStatisticsCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        }
        //:(params['state[]']?Arrays.asList(params['state[]']):'')
        /*params.SYS_QUERY_NAME = 'trackBackStatisticsCaseNum'
        def trackBackStatisticsCaseNum = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'trackBackStatisticsIpn'
        def trackBackStatisticsIpn = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'trackBackStatisticsVpn'
        def trackBackStatisticsVpn = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'trackBackStatisticsSvpn'
        def trackBackStatisticsSvpn = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'trackBackStatisticsKyh'
        def trackBackStatisticsKyh = dynamicQueryService.queryForList(params)*/
        [tableHeadList          : tableHeadList, pageSizeList: pageSizeList, pageSize: pageSize, totalCount: totalCount, startno: params.startno ?: '', endno: params.endno ?: '',
         trackBackStatistics    : trackBackStatistics, ipn: params.ipn ?: '',
//         trackBackStatisticsDocId: trackBackStatisticsDocId,
         vpn                    : params.vpn ?: '',lotId:params.lotId?:'',eqpId:params.eqpId?:'',deptName:params.deptName?:'',
         trackBackStatisticsState: trackBackStatisticsState,
         trackBackStatisticsGZZX: trackBackStatisticsGZZX,
         trackBackStatisticsKhmc: trackBackStatisticsKhmc, trackBackStatisticsQxsx: trackBackStatisticsQxsx, caseNum: params.caseNum ?: '', pono: params.pono ?: '',
         docId                  : params.docId ?: '', ipn: params.ipn ?: '', vpn: params.vpn ?: '', startTime: params.startTime ?: '', endTime: params.endTime ?: '', svpn: params.svpn ?: '', kyh: params.kyh ?: '', state: params.state ?: '', khmc: params.khmc ?: '', qxsx: params.qxsx ?: '', gzzx: params.gzzx ?: '',
         startT                 : params.startT ?: '', endT: params.endT ?: '', transType: params.transType ?: '',customerLot:params.customerLot?:'',
         trackBackStatisticsSWLX: trackBackStatisticsSWLX]
    }

    def exportExcel() {
        params.state = params.state ? Arrays.asList(params.state) : (params['state[]'] ? Arrays.asList(params['state[]']) : '')
        params.gzzx = params.gzzx ? Arrays.asList(params.gzzx) : (params['gzzx[]'] ? Arrays.asList(params['gzzx[]']) : '')
        params.transType = params.transType ? Arrays.asList(params.transType) : (params['transType[]'] ? Arrays.asList(params['transType[]']) : '')
        def sysQueryName
        sysQueryName = params.SYS_QUERY_NAME
        params.startno = 1
        params.endno = 99999999999999999
        params.SYS_QUERY_NAME = sysQueryName
        def dataList = dynamicQueryService.queryForList(params)
        def tableHeadList =  []
        if(dataList.size()>0){
            tableHeadList =  dataList[0].keySet()
        }
        String filename = new String("${sysQueryName}.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        render(template: "export${params.EXPORT_CONTENT_NAME}", model: [dataList: dataList,tableHeadList:tableHeadList,startno:params.startno?:'',endno:params.endno?:''])
    }

    def expExcel() {
        params.startno = 1
        params.endno = 99999999999999999
        params.state = params.state ? Arrays.asList(params.state) : (params['state[]'] ? Arrays.asList(params['state[]']) : '')
        params.gzzx = params.gzzx ? Arrays.asList(params.gzzx) : (params['gzzx[]'] ? Arrays.asList(params['gzzx[]']) : '')
        params.transType = params.transType ? Arrays.asList(params.transType) : (params['transType[]'] ? Arrays.asList(params['transType[]']) : '')
        params.SYS_QUERY_NAME = 'trackBackStatistics'
        def dataList = dynamicQueryService.queryForList(params)
        def tableHeadList = []
        if (dataList.size() > 0) {
            tableHeadList = dataList[0].keySet()
        }
        String filename = new String("trackBackStatistics.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        def result = g.render(template: "exp${params.EXPORT_CONTENT_NAME}", model: [dataList: dataList, tableHeadList: tableHeadList, startno: params.startno ?: '', endno: params.endno ?: ''])
        exportFileService.exportExcelbyWeb(response, "${params.SYS_QUERY_NAME}", result)
    }
}